<?php 

Class userModel extends appModel
{
	
	public function lists($page=1)
	{
		$listPage=array('data'=>'','pageLink'=>'');
		$where = " 1=1 and  p1.wxid=p2.id  ";
		$result=$this->get_page(" p1.* , p2.nickname ,p2.city ,p2.province ",$this->table('sn')." as p1 , ".$this->table('wxusers')."  as p2  ",$where,$page,'20');
		$listPage['pagelink']=$this->getpagelink();
	
	
		$i = 1;
		
	
		foreach ($result as $k => $v)
		{
	
			
			$result[$k]['order'] = $i;
			$i++;
		}
	
		$listPage['data']=$result;
	
		$sql="SELECT count(*) FROM ".$this->table('sn');
			
		$count=$this->getOne($sql);
		$listPage['count']=$count;
	
	
		return $listPage;
	}
	
	public function get_user($id)
	{
		$sql = "SELECT u.id,u.phone,u.last_login,u.integral,u.card_id FROM " . $this->table('users').
		" as u WHERE  u.id = '".$id."'" ;
		$row = $this->getRow($sql);
		return $row;
	}
	public function is_repeat($data,$name)
	{
		$sql="SELECT 1 FROM ".$this->table('users')." WHERE ".$name."='".$data."'";
		$res=$this->getOne($sql);
	
		return $res;
	}
     public function check_user($username,$password)
	{
		     $sql = "SELECT u.id,u.password,u.phone FROM " . $this->table('users').
                   " as u WHERE  u.phone = '" . $username .  "' AND u.password = '" . md5($password). "'";
		    $row = $this->getRow($sql);
            if ($row&&$username==$row['phone']&&md5($password)==$row['password'])
               {
               	$sql = "UPDATE  " . $this->table('users').
               	" SET last_login='".$_SERVER['REQUEST_TIME']."' WHERE  id = '".$row['id']."'";
               	$res = $this->query($sql);
               	
               	session('user_id',$row['id']);
               	session('username',$row['phone']);
               	cookie('QIAQIA[user_id]',   $row['id']);
               	
               	cookie('QIAQIA[user_pwd]', md5($row['password']));
               
               	return true;
               }
               else
               {
               	return false;
               }
            	
	
	}
	
	
	
}